package co.com.javeriana.aes.oms.util;

/**
 *
 * @author Jonathan Fernandez <jonathan.fvargas@gmail.com>
 */
import co.com.javeriana.aes.oms.login.LoginBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class Util {

    public static HttpSession getSession() {
        return (HttpSession) FacesContext.
                getCurrentInstance().
                getExternalContext().
                getSession(false);
    }

    public static HttpServletRequest getRequest() {
        return (HttpServletRequest) FacesContext.
                getCurrentInstance().
                getExternalContext().getRequest();
    }

    public static String getUserName() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        return session.getAttribute("username").toString();
    }

    public static String getUserId() {
        HttpSession session = getSession();
        if (session != null) {
            return (String) session.getAttribute("userid");
        } else {
            return null;
        }
    }
    
    public static String getUserPermission() {
        HttpSession session = getSession();
        if (session != null) {
            return (String) session.getAttribute("userpermission");
        } else {
            return null;
        }
    }
    
    public static Object getBean(String name) {
        return FacesContext.getCurrentInstance().getApplication().getELResolver().
                getValue(FacesContext.getCurrentInstance().getELContext(), null, name);
    }
    
    /**
     *
     * @return
     */
    public static LoginBean getUserInSession() {
        try {
            return (LoginBean) getBean("loginBean");
        } catch (Exception e) {
            return new LoginBean();
        }
    }
}
